<?php
namespace app\hotback\controller;
use think\Db;
class Slide extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 轮播列表
	 */
	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [
					'is_delete_time' => 0
				];
				$count = Db::name('slide')->where($map)->count();
				$list_data = Db::name('slide')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						//编辑url
						$list_data[$key]['save_url'] = url('Slide/saveinfo', ['id' => $value['id']]);
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}

	/**
	 * 轮播添加/修改 页面
	 */
	public function saveinfo()
	{
		// 参数
		$params = input();
		$info = [];
		if(!empty($params['id']))
		{
			//查询
			$info = Db::name('slide')->where(['id' => $params['id']])->find();
		}
		$this->assign('info',$info);

		//编辑器文件存放地址
		$this->assign('editor_path_type', 'slide');
		return $this->fetch();
	}

	/**
	 * 轮播添加/修改
	 */
	public function save()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$validate = $this->validate(
				$params,
				[
					'images_url' => 'require'
				],
				[
					'images_url.require' => '请上传图片'
				]
			);
			if(true !== $validate)
			{
				echo json_encode(['code' => -1, 'msg' => $validate]);
				exit();
			}
			//基础数据
			$data = [
				'images_url' => $params['images_url'],
				'sort' => isset($params['sort']) ? intval($params['sort']) : 0
			];
			// 添加/编辑
			if(empty($params['id']))
			{
				$data['add_time'] = time();
        		$data['upd_time'] = time();
        		$slide_id = Db::name('slide')->insertGetId($data);
			}else
			{
				$data['upd_time'] = time();
				if(Db::name('slide')->where(['id'=>intval($params['id'])])->update($data))
				{
					$slide_id = $params['id'];
				}
			}
			//是否成功
			if(isset($slide_id) && $slide_id > 0)
			{
				$result['code'] = 0;
				$result['msg'] = "操作成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "操作失败";
			}
		}
		echo json_encode($result);
	}

	/**
	 * 轮播删除
	 */
	public function delete()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$data = [
				'is_delete_time' => time(),
				'upd_time' => time()
			];
			if(Db::name('slide')->where(['id' => intval($params['id'])])->update($data))
			{
				$result['code'] = 0;
				$result['msg'] = "删除成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "删除失败";
			}
		}
		echo json_encode($result);
	}
}